package com.wandian.news.mvvm.fragmentknowledge;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.tools.GDTADTools;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.adapter.NewsKnowledgeRecycleAdapter;
import com.wandian.news.databinding.NewsKnowledgeFragmentBinding;
import com.wandian.news.databinding.NewsLifeToolFragmentBinding;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.autosize.utils.ScreenUtils;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;
import static com.mylib.libcore.config.AppConfig.GDT_APP_MB_PIC_TWO;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description KnowledgeFragment
 **/
@Route(path = RouterConfig.ROUTER_FRAGMENT_NEWS_KNOWLEDGE)
public class KnowledgeFragment extends BaseMVVMFragment<KnowledgeFragmentViewModel> {
    private NewsKnowledgeFragmentBinding dataBinding;
    private NewsKnowledgeRecycleAdapter newsKnowledgeRecycleAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.news_knowledge_fragment, container, false);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) dataBinding.viewSys.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.height = ScreenUtils.getStatusBarHeight();
        dataBinding.viewSys.setLayoutParams(linearParams);
        initRecycle();
        initViewData();
        initClink();
    }

    private void initViewData(){
        mViewModel.getData();
        mViewModel.getDataSC();
        mViewModel.getDataMJ();

        mViewModel.dataBean.observe(this,dataBean->{
            if(!UIUtils.isEmpty(dataBean)){
                dataBinding.tvTitle.setText(dataBean.getSaying()+"");
                dataBinding.tvTitleDes.setText(dataBean.getTransl()+"");
                dataBinding.tvTitleForm.setText(dataBean.getSource()+"");
            }
        });

        mViewModel.textSC.observe(this,bean->{
            if(!UIUtils.isEmpty(bean))dataBinding.tvSc.setText(bean.getContent()+"\n"+bean.getSource());
        });
        mViewModel.textMJ.observe(this,bean->{
            if(!UIUtils.isEmpty(bean))dataBinding.tvMj.setText(bean.getContent()+"\n"+bean.getSource());
        });

        //获取广告
        CSJADTools.loadCSJAdMiddleBanner(getContext(),dataBinding.rlAdBottom,"",CSJ_BANNER_ONE_ID,375,120);
        GDTADTools.loadHFAdByID(getContext(),dataBinding.flAdBottom,GDT_APP_MB_PIC_TWO,375,0 );
    }

    private void initClink(){
        dataBinding.tvItemMyjj.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","名言警句").navigation());
        dataBinding.tvItemScjs.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_SCJS).navigation());
    }

    private void initRecycle(){
        newsKnowledgeRecycleAdapter = new NewsKnowledgeRecycleAdapter(R.layout.news_item_knowledge_style);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),3);
        dataBinding.recycler.setLayoutManager(layoutManager);
        List<String> listTitle = new ArrayList<>();
        listTitle.add("近反义词");
        listTitle.add("简繁转换");
        listTitle.add("新华字典");
        listTitle.add("中文词典");
        listTitle.add("成语典故");
        listTitle.add("英汉词典");
        dataBinding.recycler.setAdapter(newsKnowledgeRecycleAdapter);
        newsKnowledgeRecycleAdapter.setNewData(listTitle);
        newsKnowledgeRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                String text = baseQuickAdapter.getData().get(i).toString();
                switch (text){
                    case "近反义词":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","近反义词").navigation();
                        break;
                    case "成语典故":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","成语典故").navigation();
                        break;
                    case "简繁转换":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","简繁转换").navigation();
                        break;
                    case "新华字典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","新华字典").navigation();
                        break;
                    case "中文词典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","中文词典").navigation();
                        break;
                    case "英汉词典":
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL).withString("title","英汉词典").navigation();
                        break;
                }
            }
        });
    }



}
